﻿@using KPIS.GERP.Models.SYS.Resources
@model KPIS.GERP.Models.ICM.PrePurchaseRequestItem
 
@{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    ViewBag.Title = "แจ้งความประสงค์ขอซื้อ/จ้าง";
    ViewBag.Descr = "ข้อมูลที่หน่วยงานต่างๆ ส่งความประสงค์ขอซื้อ/จ้างเข้ามาที่หน่วยงานพัสดุ";
}

@using (Html.BeginForm("Edit", "PrePurchaseItem", FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()

    <div class="block-button-menu-above">
        @Html.ActionLink("กลับไปหน้าแสดงข้อมูลทั้งหมด", "Index", new { PprId = Model.PprId }, new { @class = "btn btn-gray button-standard-width btn-small", role="button" })
        @Html.ButtonSubmit()
        @*@Html.ButtonCancel(new { Id = Model.PprId })*@
    </div>
    
    @Html.ValidationSummary(true, KPIS.GERP.Models.Class.ValidateText.RequiredField, new { @class = "alert alert-error" })
    @Html.Partial("_EditorForm")
}

@*
    @section scripts
    {
        <script>
            function ShowResource(haveItem) {
                var id = $('#ddlProjOperExpId').val();

                if (haveItem && id != '' && id != null) {
                    $("div #Resource").slideDown(function () {
                        var id2 = $("#lblResItemId").val();

                        if (id2 != '' && id2 != null) {
                            $("div #Amount").slideDown();
                        }
                        else {
                            $("div #Amount").slideUp();
                        }
                    });
                }
                else if (!haveItem && id != '' && id != null) {
                    $("div #Resource").slideUp(function () {
                        $("div #Amount").slideDown();
                    });
                }
                else {
                    $("div #Resource").slideUp(function () {
                        $("div #Amount").slideUp();
                    });
                }
            }

            $("form").submit(function (e) {
                e.preventDefault();
                var currentForm = this;
                bootbox.confirm(function (result) {
                    if (result) {
                        currentForm.submit();
                    }
                });
            });

            $(function () {
                ShowResource(@(Model.HaveItem));

                $("#ddlProjOperId").change(function () {
                    $('#ddlResItemId').val('');

                    var id = $('#ddlProjOperId').val();
                    $('#ddlProjOperExpId').empty();
                    if (id != '') {
                        $.getJSON('@Url.Action("GetProjectOperationExpense")/' + id, function (data) {

                            var items = '';
                            $.each(data, function (i, result) {
                                $('#ddlProjOperExpId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                            });
                        });
                    }
                    else {
                        $("div #Resource").hide("slow");
                        $("div #Amount").hide("slow");
                    }
                });

                $('#ddlProjOperExpId').change(function () {
                    $('#lblResItemId').val('');

                    var id = $('#ddlProjOperExpId').val();
                    if (id == "") {
                        $("div #Resource").hide("slow");
                        $("div #Amount").hide("slow");
                    }
                    else {
                        var lblResItemId = $('#lblResItemId');
                        var lblResItemName = $('#lblResItemName');
                        var lblResSubGrpName = $('#lblResSubGrpName');
                        var lblResGrpName = $('#lblResGrpName');
                        var lblResTypeName = $('#lblResTypeName');
                        var ddlResGrpId = $('#ddlResGrpId_chzn');
                        var ddlResSubGrpId = $('#ddlResSubGrpId_chzn');
                        var ddlResItemId = $('#ddlResItemId');

                        $.getJSON('@Url.Action("GetResourceTypeInfo")?ProjOperExpId=' + id, function (data) {
                            if (data.ProjectExpense != undefined) {
                                lblResItemId.val(data.ProjectExpense.ResItemId);
                                lblResItemName.val(data.ProjectExpense.MasResourceItem.Name);

                                if (data.ProjectExpense == undefined) alert('ProjectExpense is undefined');
                                if (data.ProjectExpense.MasResourceItem == undefined) alert('MasResourceItem is undefined');
                                if (data.ProjectExpense.MasResourceItem.MasResourceSubGroup == undefined) alert('MasResourceSubGroup is undefined');
                                if (data.ProjectExpense.MasResourceItem.MasResourceSubGroup.MasResourceGroup.MasResourceType == undefined) alert('MasResourceType is undefined');
                                lblResSubGrpName.val(data.ProjectExpense.MasResourceItem.MasResourceSubGroup.Name);
                                lblResGrpName.val(data.ProjectExpense.MasResourceItem.MasResourceSubGroup.MasResourceGroup.Name);
                                lblResTypeName.val(data.ProjectExpense.MasResourceItem.MasResourceSubGroup.MasResourceGroup.MasResourceType.Name);

                                $("#lblDescr").val(data.Descr);
                                $("#lblUnitName").val(data.ProjectExpense.MasResourceItem.MasUnit.Name);
                                $("#lblStdPrice").val(data.ProjectExpense.MasResourceItem.StdPrice);
                                $("#lblPrice").val(data.UnitPrice);
                                $("#lblQty").val(data.UnitQty);
                                $("#lblAmount").val(data.Amount);

                                $('#lblResItemName').show();
                                $('#lblResSubGrpName').show();
                                $('#lblResGrpName').show();
                                $('#ddlResGrpId_chzn').hide();
                                $('#ddlResSubGrpId_chzn').hide();
                                $('#ddlResItemId').hide();

                                ShowResource($('input[name=HaveItem]:checked').val());
                                $("div #Amount").show("slow");
                            }
                            else {
                                lblResTypeName.val(data.Name);
                                $('#ddlResSubGrpId').empty();
                                $('#ddlResItemId').empty();
                                $("#ddlResSubGrpId").trigger("liszt:updated");//update list

                                $.getJSON('@Url.Action("GetResourceGroupList")?ResTypeId=1', function (data) {// hardcode
                                    $('#ddlResGrpId').empty();
                                    var items = '';
                                    $.each(data, function (i, result) {
                                        $('#ddlResGrpId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                                    });

                                    $("#ddlResGrpId").trigger("liszt:updated");//update list
                                });

                                $("#lblDescr").val('');
                                $("#lblUnitName").val('');
                                $("#lblStdPrice").val('0');
                                $("#lblPrice").val('0');
                                $("#lblQty").val('0');
                                $("#lblAmount").val('0');

                                lblResItemName.hide();
                                lblResSubGrpName.hide();
                                lblResGrpName.hide();

                                ddlResGrpId.show();
                                ddlResSubGrpId.show();
                                ddlResItemId.show();

                                ShowResource($('input[name=HaveItem]:checked').val());
                            }
                        });
                    }
                });

                $("#ddlResGrpId").change(function () {
                    var id = $('#ddlResGrpId').val();

                    if (id != '') {
                        $.getJSON('@Url.Action("GetResourceSubGroupList")?ResGrpId=' + id, function (data) {
                            $('#ddlResSubGrpId').empty();
                            $('#ddlResItemId').empty();
                            var items = '';
                            $.each(data, function (i, result) {
                                $('#ddlResSubGrpId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                            });
                            $("#ddlResSubGrpId").trigger("liszt:updated");//update list
                        });
                    }
                    else {
                        $('#ddlResItemId').empty();
                        $("div #Amount").hide("slow");
                    }
                });

                $("#ddlResSubGrpId").change(function () {
                    var id = $('#ddlResSubGrpId').val();

                    if (id != '') {
                        $.getJSON('@Url.Action("GetResourceItemList")?ResSubGrpId=' + id, function (data) {
                            $('#ddlResItemId').empty();
                            var items = '';
                            $.each(data, function (i, result) {
                                $('#ddlResItemId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                            });
                        });
                    }
                    else {
                        $("div #Amount").hide("slow");
                    }
                });

                $("#ddlResItemId").change(function () {
                    var id = $('#ddlResItemId').val();

                    if (id != '') {
                        $("#lblResItemId").val(id);
                        $.getJSON('@Url.Action("GetResourceItemInfo")/' + id, function (data) {
                            $("#lblUnitName").val(data.MasUnit != null ? data.MasUnit.Name : "หน่วย");
                            $("#lblStdPrice").val(data.StdPrice);
                            $("#lblPrice").val(data.StdPrice);
                            $("div #Amount").show("slow");
                        });
                    }
                    else {
                        $("div #Amount").hide("slow");
                    }
                });

                $("#lblPrice").change(function () {
                    var price = $("#lblPrice").val();
                    var qty = $("#lblQty").val();
                    var lblAmount = $("#lblAmount");

                    if (price == "") {
                        price = 0;
                    }

                    if (qty == "") {
                        qty = 0;
                    }

                    lblAmount.val(price * qty);
                });

                $("#lblQty").change(function () {
                    var price = $("#lblPrice").val();
                    var qty = $("#lblQty").val();
                    var lblAmount = $("#lblAmount");

                    if (price == "") {
                        price = 0;
                    }

                    if (qty == "") {
                        qty = 0;
                    }

                    lblAmount.val(price * qty);
                });
            });
        </script>*@
    @*<script>
            $("form").submit(function (e) {
                e.preventDefault();
                var currentForm = this;
                bootbox.confirm(function (result) {
                    if (result) {
                        currentForm.submit();
                    }
                });
            });

            $(function () {
                $(".chzn-select").chosen({ width: '220px' });

                $("div #Resource").show("fast", "linear", function () {
                    if ('@(Model.ProjectOperationExpense.ProjectExpense.ResItemId.HasValue ? "1" : "0")' == '1') {
                        $('#lblResItemName').show();
                        $('#lblResSubGrpName').show();
                        $('#lblResGrpName').show();
                        $('#ddlResGrpId_chzn').hide();
                        $('#ddlResSubGrpId_chzn').hide();
                        $('#ddlResItemId').hide();
                    }
                    else {
                        $('#lblResItemName').hide();
                        $('#lblResSubGrpName').hide();
                        $('#lblResGrpName').hide();
                        $('#ddlResGrpId_chzn').show();
                        $('#ddlResSubGrpId_chzn').show();
                        $('#ddlResItemId').show();
                    }

                });
                $("div #Amount").show();

                $("#ddlProjOperId").change(function () {
                    var id = $('#ddlProjOperId').val();
                    $('#ddlProjOperExpId').empty();
                    if (id != '') {
                        $.getJSON('@Url.Action("GetProjectOperationExpense", "PrePurchaseItem")/' + id, function (data) {

                            var items = '';
                            $.each(data, function (i, result) {
                                $('#ddlProjOperExpId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                            });
                        });
                    }
                    else {
                        $("div #Resource").hide("slow");
                        $("div #Amount").hide("slow");
                    }
                });

                $('#ddlProjOperExpId').change(function () {
                    var id = $('#ddlProjOperExpId').val();
                    if (id == "") {
                        $("div #Resource").hide("slow");
                        $("div #Amount").hide("slow");
                    }
                    else {
                        var lblResItemId = $('#lblResItemId');
                        var lblResItemName = $('#lblResItemName');
                        var lblResSubGrpName = $('#lblResSubGrpName');
                        var lblResGrpName = $('#lblResGrpName');
                        var lblResTypeName = $('#lblResTypeName');
                        var ddlResGrpId = $('#ddlResGrpId_chzn');
                        var ddlResSubGrpId = $('#ddlResSubGrpId_chzn');
                        var ddlResItemId = $('#ddlResItemId');

                        $.getJSON('@Url.Action("GetResourceTypeInfo", "PrePurchaseItem")?ProjOperExpId=' + id, function (data) {
                            if (data.ProjectExpense != undefined) {
                                lblResItemId.val(data.ProjectExpense.ResItemId);
                                lblResItemName.val(data.ProjectExpense.MasResourceItem.Name);
                                lblResSubGrpName.val(data.ProjectExpense.MasResourceItem.MasResourceSubGroup.Name);
                                lblResGrpName.val(data.ProjectExpense.MasResourceItem.MasResourceSubGroup.MasResourceGroup.Name);
                                lblResTypeName.val(data.ProjectExpense.MasResourceItem.MasResourceSubGroup.MasResourceGroup.MasResourceType.Name);

                                $("#lblDescr").val(data.Descr);
                                $("#lblUnitName").val(data.ProjectExpense.MasResourceItem.MasUnit.Name);
                                $("#lblStdPrice").val(data.ProjectExpense.MasResourceItem.StdPrice);
                                $("#lblPrice").val(data.UnitPrice);
                                $("#lblQty").val(data.UnitQty);
                                $("#lblAmount").val(data.Amount);

                                $('#lblResItemName').show();
                                $('#lblResSubGrpName').show();
                                $('#lblResGrpName').show();
                                $('#ddlResGrpId_chzn').hide();
                                $('#ddlResSubGrpId_chzn').hide();
                                $('#ddlResItemId').hide();
                                $("div #Resource").show("slow");
                                $("div #Amount").show("slow");
                            }
                            else {
                                lblResTypeName.val(data.Name);
                                $('#ddlResSubGrpId').empty();
                                $('#ddlResItemId').empty();
                                $("#ddlResSubGrpId").trigger("liszt:updated");//update list

                                $.getJSON('@Url.Action("GetResourceGroupList", "PrePurchaseItem")?ResTypeId=1', function (data) {// hardcode
                                    $('#ddlResGrpId').empty();
                                    var items = '';
                                    $.each(data, function (i, result) {
                                        $('#ddlResGrpId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                                    });

                                    $("#ddlResGrpId").trigger("liszt:updated");//update list
                                });

                                $("#lblDescr").val('');
                                $("#lblUnitName").val('');
                                $("#lblStdPrice").val('0');
                                $("#lblPrice").val('0');
                                $("#lblQty").val('0');
                                $("#lblAmount").val('0');

                                lblResItemName.hide();
                                lblResSubGrpName.hide();
                                lblResGrpName.hide();

                                ddlResGrpId.show();
                                ddlResSubGrpId.show();
                                ddlResItemId.show();

                                $("div #Amount").hide("slow");
                                $("div #Resource").show("slow");
                            }
                        });
                    }
                });

                $("#ddlResGrpId").change(function () {
                    var id = $('#ddlResGrpId').val();

                    if (id != '') {
                        $.getJSON('@Url.Action("GetResourceSubGroupList", "PrePurchaseItem")?ResGrpId=' + id, function (data) {
                            $('#ddlResSubGrpId').empty();
                            $('#ddlResItemId').empty();
                            var items = '';
                            $.each(data, function (i, result) {
                                $('#ddlResSubGrpId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                            });
                            $("#ddlResSubGrpId").trigger("liszt:updated");//update list
                        });
                    }
                    else {
                        $('#ddlResItemId').empty();
                        $("div #Amount").hide("slow");
                    }
                });

                $("#ddlResSubGrpId").change(function () {
                    var id = $('#ddlResSubGrpId').val();

                    if (id != '') {
                        $.getJSON('@Url.Action("GetResourceItemList", "PrePurchaseItem")?ResSubGrpId=' + id, function (data) {
                            $('#ddlResItemId').empty();
                            var items = '';
                            $.each(data, function (i, result) {
                                $('#ddlResItemId').append("<option value='" + result.Value + "'>" + result.Text + "</option>");
                            });
                        });
                    }
                    else {
                        $("div #Amount").hide("slow");
                    }
                });

                $("#ddlResItemId").change(function () {
                    var id = $('#ddlResItemId').val();

                    if (id != '') {
                        $("#lblResItemId").val(id);
                        $.getJSON('@Url.Action("GetResourceItemInfo", "PrePurchaseItem")/' + id, function (data) {
                            $("#lblUnitName").val(data.MasUnit.Name);
                            $("#lblStdPrice").val(data.StdPrice);
                            $("#lblPrice").val(data.StdPrice);
                            $("div #Amount").show("slow");
                        });
                    }
                    else {
                        $("div #Amount").hide("slow");
                    }
                });

                $("#lblPrice").change(function () {
                    var price = $("#lblPrice").val();
                    var qty = $("#lblQty").val();
                    var lblAmount = $("#lblAmount");

                    if (price == "") {
                        price = 0;
                    }

                    if (qty == "") {
                        qty = 0;
                    }

                    lblAmount.val(price * qty);
                });

                $("#lblQty").change(function () {
                    var price = $("#lblPrice").val();
                    var qty = $("#lblQty").val();
                    var lblAmount = $("#lblAmount");

                    if (price == "") {
                        price = 0;
                    }

                    if (qty == "") {
                        qty = 0;
                    }

                    lblAmount.val(price * qty);
                });
            });
        </script>
    }*@